home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13809 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C compilers which optimize tail calls (was: GOTO controversy)
  5. Date: Wed, 10 Apr 96 11:39:30 GMT
  6. Organization: none
  7. Message-ID: <829136370snz@genesis.demon.co.uk>
  8. References: <oun34tm3c7.fsf@lynx.cs.byu.edu> <DpCv8o.9Mw@ida.liu.se> <ouivfcnaxq.fsf@lynx.cs.byu.edu> <4keb44$86h@camelot.ccs.neu.edu>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4keb44$86h@camelot.ccs.neu.edu>
  15.            will@ccs.neu.edu "William D Clinger" writes:
  16.  
  17. >Stack allocation of mutable variables and data often
  18. >interferes with proper tail recursion.  Consider
  19. >
  20. >int f (int n) {
  21. >    int A[10];
  22. >    int i;
  23. >    for (i = 0; i < 10; i = i + 1)
  24.  
  25. It is more natural to write i = i + 1 as ++i or i++ in C.
  26.  
  27. >        A[i] = n * i;
  28. >    return g (&n, &(A[0]));
  29. >}
  30. >
  31. >Yes, it is possible to write a correct C compiler that
  32. >compiles the tail-recursive call to g as a tail recursion.
  33.  
  34. What tail recursion? There's no recursion there at all. With tail recursion
  35. the function is calling itself before returning so the local variables
  36. between invocations are somewhat compatible. There was a case of mutual
  37. recursion posted earlier. This is trickier to deal with but could be reduced
  38. to simple recursion with appropriate function inlining (although the scope
  39. of that is realistically limited to fairly simple cases).
  40.  
  41. >I very much doubt whether any production C compiler
  42. >can be relied upon to generate properly tail recursive code.
  43.  
  44. Many do for cases of simple (i.e. involving a single function) tail recursion
  45. but it still remains that portable C code can't depend on it.
  46.  
  47. -- 
  48. -----------------------------------------
  49. Lawrence Kirby | fred@genesis.demon.co.uk
  50. Wilts, England | 70734.126@compuserve.com
  51. -----------------------------------------
  52.